package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.fegin.ApBehaviorEntryFegin;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author feng js
 */
@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper,ApUserSearch> implements ApUserSearchService {

    @Autowired
    private ApBehaviorEntryFegin apBehaviorEntryFegin;

    /**
     * 保存用户搜索记录
     * @param searchWords
     * @param equipmentId
     * @return
     */
    @Async("taskExecutor")
    @Override
    public void saveUserSearch(String searchWords ,Integer equipmentId) {

        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryFegin.findByUserIdOrEquipmentId(user.getId(), equipmentId);

        ApUserSearch apUserSearches = getOne(Wrappers.<ApUserSearch>lambdaQuery()
                .eq(ApUserSearch::getEntryId, apBehaviorEntry.getId()).eq(ApUserSearch::getKeyword,searchWords));
        if (apUserSearches == null) {
            ApUserSearch apUserSearch = new ApUserSearch();
            apUserSearch.setEntryId(apBehaviorEntry.getId());
            apUserSearch.setKeyword(searchWords);
            apUserSearch.setStatus(1);
            apUserSearch.setCreatedTime(new Date());
            save(apUserSearch);
        } else if (apUserSearches.getStatus() == 0) {
            ApUserSearch apUserSearch = new ApUserSearch();
            apUserSearch.setStatus(1);
            updateById(apUserSearch);
        }

    }
    /**
     * 查询搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        if (dto == null || dto.getPageSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryFegin.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Page<ApUserSearch> page = new Page<>(0, dto.getPageSize());

        LambdaQueryWrapper<ApUserSearch> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApUserSearch::getEntryId, apBehaviorEntry.getId())
                .eq(ApUserSearch::getStatus, 1);
        IPage<ApUserSearch> resultPage = page(page, lambdaQueryWrapper);

        return ResponseResult.okResult(resultPage.getRecords());
    }
    /**
     * 删除搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        update(Wrappers.<ApUserSearch>lambdaUpdate()
                .eq(ApUserSearch::getId,dto.getId())
                .set(ApUserSearch::getStatus,0));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
